<!--导入证书-->
<template>
    <a-modal v-model="showAddTable" :width="740"
             title="导入证书"
             :maskClosable="false"
             :destroyOnClose="true" @cancel="cancelApply">
        <a-form :form="form" @keydown.native.enter.prevent="saveApply" class="modelSearch">
            <template v-if="isFrom === 'ca'">
                <a-form-item label="证书类型：" v-bind="formStyle" required >
                    <a-select :getPopupContainer="trigger => trigger.parentNode"
                              placeholder="请选择证书类型"
                              v-decorator="['certType', { initialValue: '2' }, { rules: [{ required: true, message: '请选择证书类型' }] }]">
                        <a-select-option v-for="item in certTypeList" :value="item.value" :key="item.value" :disabled ="item.value === '1'" >
                            {{item.label }}
                        </a-select-option>
                    </a-select>
                </a-form-item>
            </template>
            <a-form-item label="文件：" class="handFile" v-bind="formStyle"  style="position: relative" required :extra="errorMsg">
                <a-input v-model="fileNameCert" read-only style="width: 350px; float: left"
                         disabled :maxlength="100" :placeholder="txt" />
                <a href="javascript:;">
                    <input ref="fileCert" type="file" name="signFile"
                           :accept="accept"
                           @change="onChangefileCert($event)"
                           required />选择文件
                </a>
            </a-form-item>
        </a-form>
        <div slot="footer">
            <a-button type="default" @click="cancelApply" :loading="loading">取消</a-button>
            <a-button type="primary" @click="saveApply" :loading="loading">确定</a-button>
        </div>
    </a-modal>
</template>

<script>
    import verify from 'libs/verify'

    export default {
        name: 'safeSet',
        data () {
            return {
                form: this.$form.createForm (this),
                verify,
                showAddTable: false,
                fileCert: '',
                fileNameCert: '',
                errorMsg: '',
                isFrom: '',
                loading: false,
                txt:'请选择证书pem文件',
                accept:'pem'
            }
        },
        computed: {
            formStyle () {
                return this.$store.state.common.formStyle
            },
            timeoutTime () {
                return this.$store.state.common.timeoutTime
            },
        },
        props: {
            certTypeList: {
                type: Array,
                required: true
            }
        },
        methods: {

            /**
             * Function    : change
             * Description :
             * Input       : val
             * Return      : none
             */
            change ( val ) {
                let _this = this
                setTimeout (function () {
                    if (val) {
                        _this.$refs[ 'refocus' ].focus ()
                    }
                }, 200)
            },
            /**
             * Function    : showModal
             * Description : 打开弹框
             * Input       : none
             * Return      : none
             */
            showModal ( data ) {
                this.showAddTable = true
                this.isFrom = data
                if(this.isFrom === 'ca'){
                    this.txt='请选择证书pem或p7b文件'
                    this.accept = ".pem,.p7b";
                }else{
                    this.txt='请选择证书pem文件';
                    this.accept = ".pem";
                }
                this.errorMsg = ''
            },

            /**
             * Function    : showEdit
             * Description : 编辑时单位显示
             * Input       : opt单个单位所有数据
             * Return      : none
             */
            showEdit ( opt ) {
                this.showAddTable = true
                this.id = opt.id
                this.$nextTick (() => {
                    this.form.setFieldsValue (this.$pick (opt, [ 'userName', 'name', 'sfzjh', 'phoneNum', 'orgId', 'roleId' ]))
                })
            },

            /**
             * 选择文件
             */
            onChangefileCert ( e ) {
                let _this = this
                if (e.target.files[ 0 ] === undefined) {
                    _this.fileCert = ''
                    _this.fileNameCert = ''
                } else {
                    _this.fileCert = e.target.files[ 0 ]
                    _this.fileNameCert = e.target.files[ 0 ].name
                    _this.errorMsg = ''
                    let indexDot = _this.fileNameCert.lastIndexOf ('.')
                    let type = _this.fileNameCert.substr (indexDot + 1, 3)
                    if(_this.isFrom === 'ca') {
                        if (type !== 'pem' && type !== 'p7b') {
                            _this.errorMsg = '请选择证书pem或p7b文件'
                            _this.fileCert = ''
                        } else {
                            let size = e.target.files[ 0 ].size
                            let mSize = 500 * 1024
                            if (size > mSize) {
                                _this.errorMsg = '请选择小于500KB的文件'
                                _this.fileCert = ''
                            }
                        }
                    } else {
                        if (type !== 'pem') {
                            _this.errorMsg = '请选择证书pem文件'
                            _this.fileCert = ''
                        } else {
                            let size = e.target.files[ 0 ].size
                            let mSize = 500 * 1024
                            if (size > mSize) {
                                _this.errorMsg = '请选择小于500KB的文件'
                                _this.fileCert = ''
                            }
                        }
                    }

                }
                if (this.$refs.fileCert !== undefined) {
                    this.$refs.fileCert.value = ''
                }
            },
            /**
             * Function    : saveApply
             * Description : 用户信息新增、编辑保存
             * Input       : name
             * Return      : none
             */
            saveApply ( e ) {
                e.preventDefault ()
                let _this = this
                this.form.validateFields (( err, value ) => {
                    console.log(_this.fileNameCert)
                    if (_this.fileNameCert === '') {
                        _this.errorMsg = this.isFrom ==='ca'?'请选择证书pem或p7b文件':'请选择证书pem文件'
                    }
                    if (_this.errorMsg !== undefined && _this.errorMsg !== '') {
                        return
                    }
                    if (!err) {
                        let formData = new FormData ()
                        let url = 'api/hsm/certificates/client/importClientCert'
                        if(_this.isFrom === 'ca'){
                            url = 'api/hsm/certificates/rootcert/importRootCertificate'
                            formData.append ('certType', value.certType)
                        }
                        _this.loading = true
                        formData.append ('file', _this.fileCert)
                        _this.$uploadPost (url, formData).then (res => {
                            if (res.code === 0) {
                                setTimeout(()=>{
                                    _this.$message.success (res.message)
                                    _this.loading = false
                                    _this.$emit ('init')
                                    _this.cancelApply ()
                                },_this.timeoutTime)
                            } else {
                                _this.loading = false
                                _this.fileNameCert = ''
                                _this.$message.error (res.message)
                            }
                        })
                    }
                })
            },

            /**
             * Function    : cancelApply
             * Description : 取消弹出框
             * Input       : name
             * Return      : none
             */
            cancelApply () {
                this.showAddTable = false
                let _this = this
                setTimeout (function () {
                    _this.form.resetFields ()
                    _this.isFrom = ''
                    _this.errorMsg = ''
                    _this.fileNameCert = ''
                    if (_this.$refs.fileCert !== undefined) {
                        _this.$refs.fileCert.value = ''
                    }
                }, 100)
            }
        },

        created () {}
    }
</script>
